<?php

namespace app\proxy\model\product;

use \app\common\model\product\ProxyCategory as BaseCategory;
use app\common\model\region\Region as Region1;
use app\proxy\model\proxy\Region;
use app\common\model\product\Category as ParentCategory;

/**
 * 产品分类模型
 */
class ProxyCategory extends BaseCategory
{

    /**
     * 获取列表
     * @param $param
     * @param $regionIds
     * @param $proxyId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author joey
     * @date 2024/3/28 11:38
     */
    public static function getProxyALL($param, $regionIds, $proxyId)
    {
        $model = new static;
        $data  = $model->with(['image'])
            ->when($param, function ($query) use ($param) {
                $query->where($param);
            })
            ->where(function ($query) use ($regionIds) {
                $query->whereIn('county_id', $regionIds)->whereOr("province_id", "in", $regionIds)->whereOr("city_id", "in", $regionIds);
            })
            ->where(['proxy_id'=>$proxyId])
            ->withAttr("parent_id", function ($value, $data) {
                $parent_id = ParentCategory::getParentId($data);
                return $parent_id;
            })
            ->append(["parent_id"])
            ->field('*,alias as name')
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->select();
            
        $all = !$data->isEmpty() ? $data->toArray() : [];

        foreach ($all as &$first) {
            if ($proxyId > 0) {
                $region_id = $first["province_id"];
                if ($first["city_id"]) {
                    $region_id = $first["city_id"];
                }
                if ($first["county_id"]) {
                    $region_id = $first["county_id"];
                }
                $first["region_name"] = Region1::getMergerName($region_id);
                $first["proxy_name"]  = Region::getProxyName($first, $proxyId);
                $first["can_edit"]    = $first["proxy_id"] == $proxyId ? 1 : 0;
            }
        }

        return compact('all');
    }


    /**
     * 添加新记录
     */
    public function add($data, $proxy_id)
    {
        $data['app_id']      = self::$app_id;
        $data['proxy_id']    = $proxy_id;
        $data['category_id'] = $data['category_id'][1] ?? 0;
        $data['province_id'] = $data["region_id"][0] ?? 0;
        $data['city_id']     = $data["region_id"][1] ?? 0;
        $data['county_id']   = $data["region_id"][2] ?? 0;
        unset($data["region_id"]);
        return $this->save($data);
    }

    /**
     * 编辑记录
     */
    public function edit($data, $proxyId)
    {
        $data["audit_status"] = $data['audit_status'] ?? 0;
        $data['category_id']  = is_array($data['category_id'])?($data['category_id'][1]??0):$data['category_id']??0;
        $data['province_id']  = $data["region_id"][0] ?? 0;
        $data['city_id']      = $data["region_id"][1] ?? 0;
        $data['county_id']    = $data["region_id"][2] ?? 0;
        unset($data["region_id"]);
        if ($proxyId != $this["proxy_id"]) {
            $this->error = "你无权修改此分类";
            return false;
        }

        !array_key_exists('image_id', $data) && $data['image_id'] = 0;
        return $this->save($data) !== false;
    }

    /**
     * 删除商品分类
     */
    public function remove()
    {
        return $this->delete();
    }

}
